// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

const isProduction = process.env.NODE_ENV == 'production';


const stylesHandler = MiniCssExtractPlugin.loader;



const config = {
    entry: {
        index: {
            import: './src/index.js',
            filename: 'javascript/[contenthash:10].js',
            dependOn: 'shared'
        },
        shared: ['jquery', 'vue'],
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        clean: true,
        filename: 'javascript/[contenthash:10][name].js'
            // assetModuleFilename: 'img/[contenthash:10][ext][query]'   //静态资源模块chunk打包名
    },
    devtool: 'inline-source-map',
    devServer: {
        open: true,
        host: 'localhost',
    },
    //优化配置
    optimization: {
        minimizer: [
            // 在 webpack@5 中，你可以使用 `...` 语法来扩展现有的 minimizer（即 `terser-webpack-plugin`），将下一行取消注释
            // `...`,
            new CssMinimizerPlugin(),
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
        }),

        new MiniCssExtractPlugin({
            filename: "styles/[contenthash:10][name].css",
        }),

        // Add your plugins here
        // Learn more about plugins from https://webpack.js.org/configuration/plugins/
    ],
    resolve: {
        alias: {
            'vue$': 'vue/dist/vue.min.js',
            '@': path.resolve(__dirname, 'src') // 用 webpack 1 时需用 'vue/dist/vue.common.js'
        }
    },
    module: {
        rules: [{
                test: /\.(js|jsx)$/i,
                loader: 'babel-loader',
            },
            {
                test: /\.less$/i,
                use: [stylesHandler, 'css-loader', 'postcss-loader', 'less-loader'],
            },
            {
                test: /\.css$/i,
                use: [stylesHandler, 'css-loader', 'postcss-loader'],
            },
            {
                test: /\.(png|jpg|gif)$/i,
                type: 'asset/resource',
                generator: {
                    filename: 'assets/[contenthash:10][name][ext][query]',
                },
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/i,
                type: 'asset/inline',
            },
            {
                test: /\.txt$/i,
                type: 'asset/source',
            },
            //处理html中img图片
            {
                test: /\.html$/i,
                loader: "html-loader",
            },


            // Add your rules for custom modules here
            // Learn more about loaders from https://webpack.js.org/loaders/
        ],
    },
};

module.exports = () => {
    if (isProduction) {
        config.mode = 'production';


    } else {
        config.mode = 'development';
    }
    return config;
};